﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using FineUI;

namespace lcmyjobs.Web.enprise
{
    public partial class RegisterPostJobs : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Application["webname"] != null)
                {
                    this.Title = string.Format("单位会员注册：发布职位  {0}", Application["webname"].ToString());
                }
                /*
                if (Session["enterprise_bid"] == null)
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先注册单位！", "RegisterEnterprise.aspx");
                }
                 */

                if (Session["enterprise_model"] == null)
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先填写“单位信息&bull;联系方式”！", "RegisterEnterprise.html");
                }
            }
        
        }

        protected void btn_postjob_Click(object sender, EventArgs e)
        {
            //int enterprise_bid = -1;
            //if (Session["enterprise_bid"] != null)
            //{
            //    enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);
            //}
            //Save(enterprise_bid);
            if( (Session["jobsCountRegister"] == null) || (Session["enterprise_bid"] == null) ){
                bool result = register();
                if(result==false){
                    return;
                }
            }

            
            int enterprise_bid = -1;

            if (Session["jobsCountRegister"] != null && Session["enterprise_bid"] != null)
            {
                enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                lcmyjobs.Model.jobsCount jc = Session["jobsCountRegister"] as lcmyjobs.Model.jobsCount;

                //string reardpay = this.txtrewardpay.Text;

                //Regex r = new Regex(@"^\d+(\.)?\d*$");
                //if (r.IsMatch(reardpay))
                //{
                    if (this.cbo_isfrist.Checked)//如果是急聘职位
                    {
                        #region 急聘职位会员权限计算
                        if (jc.fristJobCount - jc.isFristJobsCount > 0)//判断是否可以发布此急聘职位
                        {
                            if (Save(enterprise_bid))
                            {
                                jc.isFristJobsCount = jc.isFristJobsCount + 1;//使已发的急聘布职位加1
                                jc.jobsPubCount = jc.jobsPubCount + 1;//使已发布职位加1
                                Session["jobsCountRegister"] = jc;//记录到session中
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布急聘职位成功！", "RegisterPostJobs.html");
                            }
                            else
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布急聘职位失败！", "RegisterPostJobs.html");
                            }
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您没有权限发布急聘职位！请您升级会员后再次发布。", "ListMember.html");
                        }
                        #endregion
                    }
                    else
                    {
                        #region 非急聘职位会员权限计算
                        if (jc.jobsTotalCount - jc.jobsPubCount > 0)//判断是否可以发布此职位
                        {

                            if (Save(enterprise_bid))
                            {
                                jc.jobsPubCount = jc.jobsPubCount + 1;//使已发布职位加1
                                Session["jobsCountRegister"] = jc;//记录到session中
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布职位成功！", "RegisterPostJobs.html");
                            }
                            else
                            {
                                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布职位失败！", "RegisterPostJobs.html");
                            }
                        }
                        else
                        {
                            Session["jobsCount"] = jc;//当要跳转到单位管理中心的时候，将会员权限加上
                            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您的会员权限所能发布的职位已用完！请您升级会员后再次发布。", "ListMember.html");
                        }
                        #endregion

                    }
                //}
                //else
                //{
                //    Maticsoft.Common.MessageBox.Show(this,"悬赏金额请输入数字！");
                //}

              
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "由于长时间未操作，登陆已过期！请重新登录", "EnterpriseLogin.html");
            }
         

        }

        private bool Save(int enterprise_bid)
        {
            //int enterprise_bid = int.Parse(this.txtenterprise_bid.Text);
            //string enterprise_bh = this.txtenterprise_bh.Text;
            //int jobs_id = int.Parse(this.txtjobs_id.Text);
            string hid_jobclass = this.hid_jobclass.Value;
            string jobs_name = this.txtjob.Text;
            string jobclass = this.txtjobclass.Text;
            int workercount = int.Parse(this.txtworkercount.Text);
            string jobdep = this.txtjobdep.Text;
            string salarym = this.ddlsalarym.SelectedItem.Value;
            string salaryy = "";
            if (!string.IsNullOrEmpty(this.txtsalaryy.Text))
            {
                salaryy = this.txtsalaryy.Text;
            }

            int validity = int.Parse(this.txtvalidity.Text);
            int workerclass = -1;

            if (this.rdo_bx.Checked)
            {
                workerclass = 0;
            }
            if (this.rdo_wjs.Checked)
            {
                workerclass = 1;//往届生
            }
            if (this.rdo_yjs.Checked)
            {
                workerclass = 2;//应届生
            }

            int edus_id = int.Parse(this.ddledus_id.SelectedItem.Value);
            int workexp_id = int.Parse(this.ddlworkxp_id.SelectedItem.Value);
            string jobduty = this.txtjobduty.Text;
            string jobrequirement = this.txtjobrequirement.Text;
            DateTime releasetime = DateTime.Now;//职位发布日期
            bool isuse = true;//是否激活0、屏蔽，1、激活
            int languages_id = int.Parse(this.ddllanguage.SelectedItem.Value);
            string languages_level = this.ddllanguageslevel.SelectedItem.Value;//外语水平
            int bcount = 0;//浏览次数
            bool isfrist = false;//是否急聘0、否，1、是

            if (this.cbo_isfrist.Checked)
            {
                isfrist = true;
            }

            int status = 0;

            string area_name1 = "0";
            string area_name2 = "0";
            string area_name3 = "0";
            string marjor_name1 = "0";
            string marjor_name2 = "0";
            string marjor_name3 = "0";


            string[] area_names = this.txtarea.Text.Split(',');

            if (area_names.Length == 0)
            {
                area_name1 = this.txtarea.Text;
            }
            if (area_names.Length == 1)
            {
                area_name1 = area_names[0];
            }
            if (area_names.Length == 2)
            {
                area_name1 = area_names[0];
                area_name2 = area_names[1];
            }
            if (area_names.Length == 3)
            {
                area_name1 = area_names[0];
                area_name2 = area_names[1];
                area_name3 = area_names[2];
            }

            string majors = "";

            if (this.cbo_bx.Checked)
            {
                majors = "专业不限";
            }
            else
            {
                majors = this.txtmajor.Text;
            }

            string[] major_names = majors.Split(',');

            if (major_names.Length == 0)
            {
                marjor_name1 = this.txtmajor.Text;
            }
            if (major_names.Length == 1)
            {
                marjor_name1 = major_names[0];
            }
            if (major_names.Length == 2)
            {
                marjor_name1 = major_names[0];
                marjor_name2 = major_names[1];
            }
            if (major_names.Length == 3)
            {
                marjor_name1 = major_names[0];
                marjor_name2 = major_names[1];
                marjor_name3 = major_names[2];
            }

            float rewardmoney = -1f;
          
            if (this.cbo_isreward.Checked)
            {
                rewardmoney = float.Parse(this.txtrewardpay.Text);
            }



            lcmyjobs.Model.enterprise_releasejob model = new lcmyjobs.Model.enterprise_releasejob();
            model.enterprise_bid = enterprise_bid;
            //model.enterprise_bh = enterprise_bh;
            //model.jobs_id = jobs_id;
            model.jobs_name = jobs_name;
            model.jobclass = hid_jobclass;//工作类别父类名
            model.job_child_class = jobclass;//工作类别子类名
            model.workercount = workercount;
            model.jobdep = jobdep;
            model.salarym = salarym;
            model.salaryy = salaryy;
            model.validity = validity;
            model.workerclass = workerclass;
            model.edus_id = edus_id;
            model.workexp_id = workexp_id;
            model.jobduty = jobduty;
            model.jobrequirement = jobrequirement;
            model.releasetime = releasetime;
            model.isuse = isuse;
            model.languages_id = languages_id;
            model.bcount = bcount;
            model.isfrist = isfrist;
            model.status = status;
            model.languages_level = languages_level;
            model.area_name1 = area_name1;
            model.area_name2 = area_name2;
            model.area_name3 = area_name3;
            model.marjor_name1 = marjor_name1;
            model.marjor_name2 = marjor_name2;
            model.marjor_name3 = marjor_name3;
            model.rewardmoney = rewardmoney;

            lcmyjobs.BLL.enterprise_releasejob bll = new lcmyjobs.BLL.enterprise_releasejob();
            if (bll.Add(model) > 0)
            {
                return true;
                //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布职位成功！", "RegisterPostJobs.aspx");
            }
            else
            {
                return false;
                //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布职位失败！请重新填写保存。", "RegisterPostJobs.aspx");
            }
        }

      
        protected void btn_postagin_Click(object sender, EventArgs e)
        {
        
             if( (Session["jobsCountRegister"] == null) || (Session["enterprise_bid"] == null)){
                 bool result = register();
                 if(result==false){
                     return;
                 }
             }

             int enterprise_bid = -1;

             if (Session["jobsCountRegister"] != null && Session["enterprise_bid"] != null)
             {
                 enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);
                 lcmyjobs.Model.jobsCount jc = Session["jobsCountRegister"] as lcmyjobs.Model.jobsCount;

                 lcmyjobs.BLL.enterprise_releasejob rjob = new BLL.enterprise_releasejob();

                 DataSet ds_rjob = rjob.GetList(" enterprise_bid=" + enterprise_bid);//判断企业是否发布了职位

                 if (ds_rjob.Tables[0].Rows.Count > 0)
                 {
                     // 是否正在填写，发布职位
                     if (this.txtjob.Text != "")
                     {
                         Maticsoft.Common.MessageBox.Show(this, "您正在发布职位！请点击发布职位后，再进入个人管理中心！");
                     }
                     else
                     {
                         Session["lastlogin"] = DateTime.Now.ToString();
                         Session["jobsCountRegister"] = jc;//记录到session中
                         Response.Redirect("EnterpriseIndex.html");
                     }
                 }
                 else
                 {
                     Maticsoft.Common.MessageBox.Show(this, "请先填写职位信息并点击继续发布职位，成功发布职位后可进入单位管理中心!");
                 }


               

                 //if (this.cbo_isfrist.Checked)//如果是急聘职位
                 //{
                 //    #region 急聘职位会员权限计算
                 //    if (jc.fristJobCount - jc.isFristJobsCount > 0)//判断是否可以发布此急聘职位
                 //    {
                 //        if (Save(enterprise_bid))
                 //        {
                 //            Session["lastlogin"] = DateTime.Now.ToString();

                 //            jc.isFristJobsCount = jc.isFristJobsCount + 1;//使已发的急聘布职位加1
                 //            jc.jobsPubCount = jc.jobsPubCount + 1;//使已发布职位加1
                 //            Session["jobsCountRegister"] = jc;//记录到session中
                 //            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布急聘职位成功！", "EnterpriseIndex.aspx");
                 //        }
                 //        else
                 //        {
                 //            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布急聘职位失败！", "EnterpriseIndex.aspx");
                 //        }
                 //    }
                 //    else
                 //    {
                 //        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您没有权限发布急聘职位！请您升级会员后再次发布。", "ListMember.aspx");
                 //    }
                 //    #endregion
                 //}
                 //else
                 //{
                 //    #region 非急聘职位会员权限计算
                 //    if (jc.jobsTotalCount - jc.jobsPubCount > 0)//判断是否可以发布此职位
                 //    {

                 //        if (Save(enterprise_bid))
                 //        {
                 //            Session["lastlogin"] = DateTime.Now.ToString();

                 //            jc.jobsPubCount = jc.jobsPubCount + 1;//使已发布职位加1
                 //            Session["jobsCountRegister"] = jc;//记录到session中
                 //            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布职位成功！", "EnterpriseIndex.aspx");
                 //        }
                 //        else
                 //        {
                 //            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "发布职位失败！", "EnterpriseIndex.aspx");
                 //        }
                 //    }
                 //    else
                 //    {
                 //        Session["jobsCount"] = jc;//当要跳转到单位管理中心的时候，将会员权限加上
                 //        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您的会员权限所能发布的职位已用完！请您升级会员后再次发布。", "ListMember.aspx");
                 //    }
                 //    #endregion

                 //}
             }
             else
             {
                 Maticsoft.Common.MessageBox.ShowAndRedirect(this, "由于长时间未操作，请用您注册的单位会员账号登陆单位会员管理中心发布职位", "EnterpriseLogin.html");
             }

            
        }


        private bool register()
        {
            bool result = false;
            if (Session["enterprise_model"] != null)
            {
                lcmyjobs.BLL.enterprise_basic bm = new BLL.enterprise_basic();
                lcmyjobs.Model.enterprise_basic model = Session["enterprise_model"] as lcmyjobs.Model.enterprise_basic;

                int enterprise_bid = bm.Add(model);

                if (enterprise_bid > 0)
                {
                    lcmyjobs.Model.jobsCount jc = new Model.jobsCount();

                    jc.jobsTotalCount = 5;
                    jc.jobsPubCount = 0;
                    jc.isFristJobsCount = 0;
                    jc.fristJobCount = 0;

                    Session["jobsCountRegister"] = jc;//记录刚注册会员(试用会员)的权限
                    Session["enterprise_bid"] = enterprise_bid;
                    Session["enterprise_name"] = model.name;
                    result = true;
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存失败！请重新填写保存。", "RegisterEnterprise.html");
                }
            }
            return result;
        } // end of register

        protected void btn_why_Click(object sender, EventArgs e)
        {

            //Alert alert = new Alert();
            string msg = "<div style='width:250px;text-indent:2em; line-height:20px;'><p style='padding-Bottom:5px; '>  悬赏招聘：更准确、更快捷、更低成本。</p>";
            msg += "<p style='padding-Bottom:5px; '>  更准确----提供专业的人才选择工具，帮助企业深度甄选人才。 基于人才胜任素质的评测工具辅助人才选择。颠覆被动“等待”人才的传统方式。</p>";
            msg += "<p style='padding-Bottom:5px; '>  更快捷----发挥社会力量，动员群众，借助猎手人脉资源关系，让您的悬赏信息迅速覆盖目标群体，快速找到适合人才。</p>";
            msg += "<p>  更低成本----悬赏招聘使企业能够以较低的价格完成猎头服务。悬赏企业自行定价、发布有效职位，成本仅为传统猎头费用(费用为该职位年薪的30%)的十分之一，甚至更低，并全程享受专业人事招聘服务，轻松猎才。</p></div>";

            /*
            alert.Message = msg;
            alert.Icon = Icon.Help;
            alert.Title = "为什么要悬赏招聘";
            alert.Show();
            */
            Alert.Show(msg);
        }
    }
}